<?php
/**
 *
 * @Author nikm $
 *
 * @Date 2012-04-01 $
 */

if(!defined('FROM_INDEX')) exit('Invalid request');

require ROOT_PATH.'module/admin/master.mod.php';
require_once ROOT_PATH.'include/lib/image.func.php';

class moduleObj extends masterObj
{
	private $shop = '';
	
	function __construct($request)
	{
		parent::__construct($request);
		
		$sid = isset($this->request['sid']) ? (int)$this->request['sid'] : '';
		
		if(!$sid)
		{
			$this->messager("请指定免税店", 'manage.php?m=shop');
		}
		
		$info = get_shop_info(array('sid'=>$sid));
			
		if(!isset($info['id']) || $info['id'] < 1)
		{
			$this->messager("免税店不存在", 'manage.php?m=shop');
		}
		
		$this->shop = $info;
		
		$this->excute();
	}
	
	private function excute()
	{
		ob_start();
		
		switch($this->act)
		{
			case 'add':
				$this->add();
				break;
			case 'do_add':
				$this->doAdd();
				break;
			case 'do_delete':
				$this->doDelete();
				break;
			case 'modify':
				$this->modify();
				break;
			case 'do_modify':
				$this->doModify();
				break;
			case 'comment_list':
				$this->commentList();
				break;
			case 'delete_comment':
				$this->deleteComment();
				break;
			default:
				$this->main();
				break;
		}
		
		$body = ob_get_clean();
		
		$this->showBody($body);
	}
	
	private function main()
	{
		$info = $this->shop;
		$list = get_shop_item_list(array('sid'=>$info['id']));
				
		include template('admin/shop_item');
	}
	
	private function add()
	{
		$info = array();
		if(isset($this->request['c']))
		{
			$info = cache_get($this->request['c']);
			cache_delete($this->request['c']);
		}
		
		$info['shop_name'] = $this->shop['name'];
		$info['name'] = isset($info['name']) ? $info['name'] : '';
		$info['product_place'] = isset($info['product_place']) ? $info['product_place'] : '';
		$info['desc'] = isset($info['desc']) ? $info['desc'] : '';
		
		$info['market_price'] = isset($info['market_price']) ? $info['market_price'] : '';
		$info['duty_free_price'] = isset($info['duty_free_price']) ? $info['duty_free_price'] : '';
		$info['up'] = '0';
		$info['down'] = '0';
		
		$action = 'do_add&sid='.$this->shop['id'];
		$action_name = '添加商品';
		include template('admin/shop_item_info');
	}
	
	private function doAdd()
	{
		//DBFunction::getInstance()->setDebug();
		
		$request['uid'] = $this->user['id'];
		$request['sid'] = $this->shop['id'];
		$request['name'] = isset($this->request['name']) ? $this->request['name'] : '';
		
		$request['product_place'] = isset($this->request['product_place']) ? $this->request['product_place'] : '';
		$request['desc'] = isset($this->request['desc']) ? $this->request['desc'] : '';
		
		$request['market_price'] = isset($this->request['market_price']) ? (float)$this->request['market_price'] : 0;
		$request['duty_free_price'] = isset($this->request['duty_free_price']) ? (float)$this->request['duty_free_price'] : 0;
		
		$request['image'] = '';
		
		$key = md5(implode(',', $request));
		cache_set($key, $request);
		
		$redirect = "manage.php?m=shop_item&a=add&sid=".$request['sid']."&c=$key";
		
		if(!$request['name'])
        {
			$this->messager("内容不能为空", $redirect);
		}
				
		if($_FILES['image']['name'])
		{
			$image_dir = 'data/images/'.rand(1, 99).'/';
			$image_path = move_image_to_path('image', $image_dir);
			if($image_path)
			{
				$request['image'] = $image_path;
			}
		}
		
		$result = add_shop_item($request);
		
		if($result)
		{
			$this->messager("添加成功", 'manage.php?m=shop_item&a=modify&sid='.$request['sid'].'&id='.$result);
		}
		else
		{
			$this->messager("添加失败", $redirect);
		}
	}
	
	private function doDelete()
	{
		//DBFunction::getInstance()->setDebug();
		
		$ids = isset($this->request['ids']) ? $this->request['ids'] : '';
		if(check_array($ids))
		{
			$ids = implode(',', $ids);
		}
		
		$redirect = "manage.php?m=shop_item&sid=".$this->shop['id'];
		
		if(!$ids)
		{
			$this->messager("请指定ID", $redirect);
		}
		
		$result = delete_shop_item(array('sid'=>$this->shop['id'], 'ids'=>$ids));

		if($result)
		{
			$this->messager("删除成功", $redirect);
		}
		else
		{
			$this->messager("删除失败", $redirect);
		}
	}
	
	private function modify()
	{
		$id = isset($this->request['id']) ? (int)$this->request['id'] : '';
		
		if(!$id)
		{
			$this->messager("请指定ID", 'manage.php?m=shop_item&sid='.$this->shop['id']);
		}
		
		if(isset($this->request['c']) && ($info = cache_get($this->request['c'])) !== false)
		{
			cache_delete($this->request['c']);
		}
		else
		{
			$info = get_shop_item(array('id'=>$id, 'sid'=>$this->shop['id']));
			if(!isset($info['id']) || $info['id'] < 1)
			{
				$this->messager("商品不存在", 'manage.php?m=shop_item&sid='.$this->shop['id']);
			}
		}
		
		$info['shop_name'] = $this->shop['name'];
		
		$action = 'do_modify&sid='.$this->shop['id'].'&id='.$id;
		$action_name = '编辑免税店商品';
		include template('admin/shop_item_info');
	}
	
	private function doModify()
	{
		//DBFunction::getInstance()->setDebug();
		
		$id = isset($this->request['id']) ? (int)$this->request['id'] : '';
		
		if(!$id)
		{
			$this->messager("请指定ID", 'manage.php?m=shop_item&sid='.$this->shop['id']);
		}
		
		$info = get_shop_item(array('id'=>$id, 'sid'=>$this->shop['id']));
		if(!isset($info['id']) || $info['id'] < 1)
		{
			$this->messager("商品不存在", 'manage.php?m=shop_item&sid='.$this->shop['id']);
		}
		
		$request['uid'] = $this->user['id'];
		$request['sid'] = $this->shop['id'];
		$request['id'] = $id;
		
		$request['name'] = isset($this->request['name']) ? $this->request['name'] : '';
		
		$request['product_place'] = isset($this->request['product_place']) ? $this->request['product_place'] : '';
		$request['desc'] = isset($this->request['desc']) ? $this->request['desc'] : '';
		
		$request['market_price'] = isset($this->request['market_price']) ? (float)$this->request['market_price'] : 0;
		$request['duty_free_price'] = isset($this->request['duty_free_price']) ? (float)$this->request['duty_free_price'] : 0;
		$request['image'] = '';
				
		$key = md5(implode(',', $request));
		cache_set($key, $request);
		
		$redirect = "manage.php?m=shop_item&a=add&sid=".$request['sid']."&c=$key";
		
		if(!$request['name'])
        {
			$this->messager("内容不能为空", $redirect);
		}
		
		$request['uid'] = $this->user['id'];

		if($_FILES['image']['name'])
		{
			$old_image = $info['image'];
			
			if(is_file($old_image))
			{
				delete_image($old_image);
			}
			
			$image_dir = 'data/images/'.rand(1, 99).'/';
			$image_path = move_image_to_path('image', $image_dir);
			if($image_path)
			{
				$request['image'] = $image_path;
			}
		}
		else
		{
			$request['image'] = $info['image'];
		}
		
		$result = update_shop_item($request);
		
		if($result)
		{
			$this->messager("修改成功", "manage.php?m=shop_item&sid=".$request['sid']);
		}
		else
		{
			$this->messager("修改失败", "manage.php?m=shop_item&sid=".$request['sid']);
		}
	}
	
	private function commentList()
	{
		//DBFunction::getInstance()->setDebug();
		
		$page = $this->request['offset'] + 1;
		
		$id = isset($this->request['id']) ? (int)$this->request['id'] : '';
		$query_link = "manage.php?m=shop_item&a=comment_list&id=".$id;
		
		if(!$id)
		{
			$this->messager("请指定ID", 'manage.php?m=shop_item');
		}
		
		$info = get_shop_item(array('id'=>$id, 'sid'=>$this->shop['id']));
		
		if(!isset($info['id']) || $info['id'] < 1)
		{
			$this->messager("商品不存在", 'manage.php?m=shop_item');
		}
		
		$result = get_shop_item_comment_list(array('id'=>$id, 'limit'=>$this->request['limit'], 'offset'=>$this->request['offset']));
		$list = $result['result'];
		
		$total_record = $result['count'];
		$page_html = page($total_record, $this->request['limit'], $page, $query_link, '10 20 50 100 200 500');
		
		$shop_id = $this->shop['id'];
		
		include template('admin/shop_item_comment_list');
	}
	
	private function deleteComment()
	{
		//DBFunction::getInstance()->setDebug();
		
		$sid = isset($this->request['id']) ? (int)$this->request['id'] : '';
		$ids = isset($this->request['ids']) ? $this->request['ids'] : '';
				
		$redirect = "manage.php?m=shop_item&a=comment_list&id=".$sid;
		
		if(!$ids)
		{
			$this->messager("请指定ID", $redirect);
		}
				
		$result = false;
		if(check_array($ids))
		{
			foreach($ids as $id)
			{
				$result = delete_shop_item_comment(array('id'=>$id));
			}
		}
		else
		{
			$result = delete_shop_item_comment(array('id'=>$ids));
		}
				
		if($result)
		{
			$this->messager("删除成功", $redirect);
		}
		else
		{
			$this->messager("删除失败", $redirect);
		}
	}
}